package cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.sql;

import cn.iocoder.yudao.framework.common.genCode.common.StringBuilderHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenColumnDO;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenTableDO;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.GlobalBinding;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.LocalBinding;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.handler.CommonColumn;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.handler.PathHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.CodeGenTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class H2 implements CodeGenTemplate {

    @Autowired
    CommonColumn commonColumn;

    public String getPath(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        return "sql/" + table.getTableName() + "/h2.sql";
    }

    public String render(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        StringBuilderHandler code = new StringBuilderHandler();
        code.apAndBl("-- 将该建表 SQL 语句，添加到 yudao-module-" + table.getModuleName() + "-impl 模块的 test/resources/sql/create_tables.sql 文件里");
        code.apAndBl("CREATE TABLE IF NOT EXISTS \"" + table.getTableName() + "\" (");
        for (XiaoBizHandlerCodegenColumnDO column : columns) {
            if (column.getPrimaryKey()) {
                // 处理主键
                code.ap("    \"" + column.getJavaField() + "\"");
                if ("String".equals(column.getJavaType())) {
                    code.ap(" " + column.getColumnType() + " NOT NULL");
                } else {
                    code.ap(" " + column.getColumnType() + " NOT NULL GENERATED BY DEFAULT AS IDENTITY");
                }
                code.apAndBl(",");
            } else {
                // 处理非主键
                String columnName = column.getColumnName();
                switch (columnName) {
                    case "create_time":
                        code.apAndBl("    \"create_time\" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,");
                        break;
                    case "update_time":
                        code.apAndBl("    \"update_time\" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,");
                        break;
                    case "creator":
                    case "updater":
                        code.apAndBl("    \"" + column.getColumnName() + "\" " + column.getColumnType() + " DEFAULT '',");
                        break;
                    case "deleted":
                        code.apAndBl("    \"deleted\" bit NOT NULL DEFAULT FALSE,");
                        break;
                    default: {
                        code.ap("    \"" + column.getColumnName() + "\" " + column.getColumnType());
                        if (!column.getNullable())
                            code.ap(" NOT NULL");
                        code.apAndBl(",");
                        break;
                    }
                }
            }
        }
        code.apAndBl("    PRIMARY KEY (\"" + primaryColumn.getColumnName() + "\")");
        code.apAndBl(") COMMENT '" + table.getTableComment() + "';");
        code.bl();
        code.apAndBl("-- 将该删表 SQL 语句，添加到 yudao-module-" + table.getModuleName() + "-impl 模块的 test/resources/sql/clean.sql 文件里");
        code.apAndBl("DELETE FROM \"" + table.getTableName() + "\";");
        code.bl();
        return code.toString();
    }
}
